package com.itany.netclass.service.proxy;

import com.itany.mvc.util.CommonsMultipartFile;
import com.itany.netclass.entity.Chapter;
import com.itany.netclass.exception.*;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.ChapterService;
import com.itany.netclass.tran.TransactionManager;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:2021/10/16 18:49
 * Version:1.0-SNAPSHOT
 */
public class ChapterServiceProxy implements ChapterService {
    @Override
    public List<Chapter> findAllChapterByCourseId(String courseId) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            List<Chapter> list = chapterService.findAllChapterByCourseId(courseId);
            tran.commit();
            return list;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Chapter> findChapterByForm(String courseId,String chapterName, String chapterInfo, String kaishitime, String jieshutime, String status) throws NullSringException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            List<Chapter> chapterByForm = chapterService.findChapterByForm(courseId, chapterName, chapterInfo, kaishitime, jieshutime, status);
            tran.commit();
            return chapterByForm;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void yongChapterById(String courseId,String chapterId, String status) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.yongChapterById(courseId,chapterId,status);
            tran.commit();
        } catch (ServiceException e) {
           tran.rollback();
           throw e;
        } catch (Exception e) {
            tran.rollback();
            throw  e;
        }

    }

    @Override
    public void jianchaTitle(String chapterId,String courseId, String title) throws ServiceException, NullSringException, CharpetExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.jianchaTitle(chapterId,courseId,title);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (CharpetExistException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void jianchaInfo(String info) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.jianchaInfo(info);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void jianchaZhiyuantitle(String zhiyuantitle) throws ServiceException, NullSringException, ResourceExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.jianchaZhiyuantitle(zhiyuantitle);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (ResourceExistException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void jianchaCostNum(String costNum) throws ServiceException, NullSringException, TpyeException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.jianchaCostNum(costNum);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (TpyeException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void jianchaResource(List<CommonsMultipartFile> list, String resourceType) throws ServiceException, NullSringException, BuPiPeiException, TpyeException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.jianchaResource(list,resourceType);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();;
            throw e;
        } catch (NullSringException e) {
            tran.rollback();;
            throw e;
        } catch (BuPiPeiException e) {
            tran.rollback();;
            throw e;
        } catch (TpyeException e) {
            tran.rollback();;
            throw e;
        }

    }

    @Override
    public void addChapterAndResource(HttpSession session, String title, String info, String resourceTitle, String costType, String costNum, String resourceType, List<CommonsMultipartFile> list) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            chapterService.addChapterAndResource(session,title,info,resourceTitle,costType,costNum,resourceType,list);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (Exception e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public Chapter findChapterAndResourceByChapterId(String chapterId) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.begin();
            Chapter chapter = chapterService.findChapterAndResourceByChapterId(chapterId);
            tran.rollback();
            return chapter;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public void modifyChapterAndResource(HttpSession session, String chapterId, String title, String info, String resourceTitle, String costType, String costNum, String resourceType, List<CommonsMultipartFile> list) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ChapterService chapterService= (ChapterService) ObjectFactory.getObject("ChapterServiceTarget");
        try {
            tran.commit();
            chapterService.modifyChapterAndResource(session,chapterId,title,info,resourceTitle,costType,costNum,resourceType,list);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (Exception e) {
            tran.rollback();
            throw e;
        }
    }


}
